const mongoose=require("mongoose")
//暴露第三层函数
module.exports={
    getCar3,
    getCarDetail3,
    searchCars3,
    add3,
    upDate3,
    userSearch3,
    upDateRemoved3
}
//分页获取车辆数据
async function getCar3({start,count}){
    // console.log("第三层"+start,count);
    
    const message=await mongoose.model("carModel").find().limit(~~count).skip(~~start).populate("userId");
    const total=await mongoose.model("carModel").find().countDocuments();
    return {message,total}
}

//获取车辆详细信息
async function getCarDetail3({_id}){
    // console.log("第三层"+_id);    
    const message=await mongoose.model("carModel").find({_id});
    return message
}

//搜索车辆
async function searchCars3({selectedType,selectedValue}){
    const message=await mongoose.model("carModel").find({[selectedType]:{$regex:selectedValue,$options:"$i"}});
    const total=await mongoose.model("carModel").find({[selectedType]:{$regex:selectedValue,$options:"$i"}}).countDocuments();
    return {message,total}
}

//用户查询自己的车辆
async function userSearch3({userId}){
    const message=await mongoose.model("carModel").find({userId});
    const total=await mongoose.model("carModel").find({userId}).countDocuments();
    return {message,total}
}

//新增车辆
async function add3(data){
    const result=await mongoose.model("carModel").create(data);
    return result
}
//修改状态
async function upDate3({_id,status}){
    // console.log("第三层"+_id,status);
    const result=await mongoose.model("carModel").updateOne({_id},{status})
    return result
}
//下架车辆
async function upDateRemoved3({_id}){
    const result=await mongoose.model("carModel").updateOne({_id},{isRemoved:true,status:0});
    return result
}